/*
 * Copyright 2020-2021 the original author or authors.
 *
 * Licensed under the General Public License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.gnu.org/licenses/gpl-3.0.en.html
 */

package team.seekers.oj.group.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.util.CollectionUtils;
import team.seekers.oj.common.converter.BaseConvertUtils;
import team.seekers.oj.common.util.SpringContextUtils;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description DTO-DO 特殊转换方法的统一收口处
 * @author hbc
**/
public class GroupConvertUtils extends BaseConvertUtils {

    public static List<Long> groupUsersToUserIdList(byte[] users) {
        if (users == null) {
            return Lists.newArrayList();
        }
        ByteBuffer wrap = ByteBuffer.wrap(users);
        List<Long> groupUserIdList = new ArrayList<>(users.length / 8);
        for (int i = 0, n = users.length; i < n; i += 8) {
            groupUserIdList.add(wrap.getLong(i));
        }
        return groupUserIdList;
    }

    public static byte[] groupUsersFromUserIdList(List<Long> groupUserIdList) {
        ByteBuf byteBuf = Unpooled.buffer(groupUserIdList.size() * 8);
        groupUserIdList.forEach(byteBuf::writeLong);
        return byteBuf.array();
    }
}